<template>
    <a-form ref='formRef' :label-col='{span : 5}' :wrapper-col="{ span: 10 }" :model='formData'>
        <a-form-item label='佣金比例' name='commissionRate' :rules="[{required: true, message: '请填写佣金比例'}]">
            <a-input-number type='number' :min="0" :max="100" v-model:value='formData.commissionRate'>
                <template #addonAfter>%</template>
            </a-input-number>
        </a-form-item>
        <a-form-item label='提现手续费' name='withdrawalFeeRate' :rules="[{required: true, message: '请填写提现手续费'}]">
            <a-input-number type='number' :min="0" :max="100" v-model:value='formData.withdrawalFeeRate'>
                <template #addonAfter>%</template>
            </a-input-number>
        </a-form-item>
        <a-form-item label='兑换手续费' name='exchangeFeeRate' :rules="[{required: true, message: '请填写兑换手续费'}]">
            <a-input-number type='number' :min="0" :max="100" v-model:value='formData.exchangeFeeRate'>
                <template #addonAfter>%</template>
            </a-input-number>
        </a-form-item>
        <a-form-item :wrapper-col="{ offset: 5, span: 10 }">
            <a-button type="primary" @click="submit">保存</a-button>
        </a-form-item>

    </a-form>
</template>

<script lang="ts" setup>
        import { ref } from 'vue';
import { useI18nStore } from '@/store/i18n';
import { getConfigApi, setConfigApi } from '@/api/Distribution';
import { message } from 'ant-design-vue';

    const i18n = useI18nStore()

    const props = defineProps<{
        row? : any;
        parentId:number;
    }>()

    const expireTypes = ref<any[]>([])

    const formRef = ref()

    
    const formData = ref({
        commissionRate : '',
        withdrawalFeeRate : '',
        exchangeFeeRate : '',
    })


    if(props.row) {
        for(let k in props.row) {
            if(k in formData.value) {
                formData.value[k] = props.row[k]
            }
        }

        
    }




    async function submit() {
        await formRef.value.validate()
    
        const r = await setConfigApi(formData.value)
        if(r.success) {
            message.success('保存成功')
        }else {
            message.success('保存失败')
        }
    }


    getConfigApi().then(r => {
        if(!r) return
        for(let k in r) {
            if(k in formData.value) {
                formData.value[k] = r[k]
            }
        }
        
    })
</script>